const query = require('../db');
const util = require('../util');
module.exports = {
    //添加训练
    Status:1,
    addTrining(data,callback){
         let PK_TrainingGuid = util.createGuid();
         let CreateTime = util.getNowTime();
         let FK_ClassGuid = data.FK_ClassGuid?data.FK_ClassGuid:null;
         let sql =`INSERT INTO training VALUES('${PK_TrainingGuid}','${data.FK_PaperGuid}','${data.TrainingName}','${data.Description}',${CreateTime},'${data.Creater}',${data.InviteID},${this.Status},'${FK_ClassGuid}')`;
         query(sql,(err,data)=>{
             if(err){
                 util.writeErr(err+',sql='+sql+'时间='+new Date+',');
             }else{
                 callback(PK_TrainingGuid);
             }
         })
    },
    //获取训练，参数解释：id:训练的Guid如果有查找指定ID，没有查找所有的训练，status:0代表已删除，1代表未删除的,不传默认为1
    getTriningByStatus(id,status,callback){
            if(typeof(status)=='undefined') status=1;
            let sql = `SELECT * FROM training WHERE Status = ${status}`
            if(id) sql+=` AND PK_TrainingGuid = '${id}'`;
            query(sql,(err,data)=>{
                if(err){
                     util.writeErr(err+',sql='+sql+'时间='+new Date+',');
                }else{
                    callback(data);
                }
            })
    },
    //根据训练ID获取训练
    getTraining(id,callback){
        let sql = `SELECT * FROM training WHERE PK_TrainingGuid = '${id}' `;
        query(sql,(err,data)=>{
            if(err){
                util.writeErr(err+',sql='+sql+'时间='+new Date+',');
            }else{
                callback(data);
            }
        })
    },
    //删除训练（根据训练的Guid）
    delTraining(PK_TrainingGuid,callback){
        let sql = `UPDATE training SET \`Status\` = 0 WHERE PK_TrainingGuid = '${PK_TrainingGuid}' `;
        query(sql,(err,data)=>{
             if(err){
                  util.writeErr(err+',sql='+sql+'时间='+new Date+',');
             }else{
                 this.getTrining(PK_TrainingGuid,0,callback);
             }
        })
    },
    //根据训练Guid修改训练
    modifyTrainingById(id,data,callback){
        let FK_ClassGuid = data.FK_ClassGuid?data.FK_ClassGuid:null;
         let CreateTime = util.getNowTime();
        let sql =`UPDATE training SET TrainingName='${data.TrainingName}',Description='${data.Description}',Creater='${data.Creater}',InviteID='${data.InviteID}',FK_ClassGuid='${FK_ClassGuid}',CreateTime=${CreateTime} WHERE PK_TrainingGuid='${id}'`;
        query(sql,(err,data)=>{
            if(err){
                 util.writeErr(err+',sql='+sql+'时间='+new Date+',');
            }else{
                this.getTrining(id,1,callback);
            }
        })
    },
    //根据创建者获取训练，参数解释：id:创建者Guid（必传），status:0代表已删除，1代表未删除的,不传默认为1
    getTriningOfCreater(id,status,callback){
         if(typeof(status)=='undefined') status=1;
         let sql = `SELECT * FROM training WHERE Status = ${status} AND Creater = '${id}'`;
         query(sql,(err,data)=>{
             if(err){
                 util.writeErr(err+',sql='+sql+'时间='+new Date+',');
             }else{
                 callback(data);
             }
         })
    },
    //根据问卷Guid和创建者获取训练，参数解释：id:训练Guid（必传）,Creater:创建者的Guid，status:0代表已删除，1代表未删除的,不传默认为1
    getTriningOfPaper(id,status,Creater,callback){
        if(typeof(status)=='undefined') status=1;
        let sql = `SELECT * FROM training WHERE Status = ${status} AND FK_PaperGuid = '${id}' AND Creater = '${Creater}'`;
        query(sql,(err,data)=>{
             if(err){
                  util.writeErr(err+',sql='+sql+'时间='+new Date+',');
             }else{
                 callback(data);
             }
        })
    },
    //判断用户是否有删除，修改训练的权限，参数解释,ids训练的GUID,传入数组，userID，用户的Guid
   userAccredit(ids,userID,callback){
       let results = [];
       let sql =`SELECT * FROM training WHERE Creater = '${userID}'`
       let VALUES ="";
       ids.forEach(function(val,index) {
           VALUES+=`"${val}",`
       });
      VALUES = VALUES.substring(0,VALUES.length-1);
        sql+=` AND PK_TrainingGuid IN (${VALUES})`
           query(sql,(err,data)=>{
                        if(err){
                            util.writeErr(err+',sql='+sql+'时间='+new Date+',');
                        }else{
                             callback(data);
                        }
                    })
   },
//    根据测试Guid恢复训练
 reCoverTrainingById(id,callback){
     let sql = `UPDATE training SET \`Status\` =1 WHERE PK_TrainingGuid='${id}'`;
     query(sql,(err,data)=>{
         if(err){
              util.writeErr(err+',sql='+sql+'时间='+new Date+','); 
         }else{
             this.getTrining(id,1,callback);
         }
     })
 },
 //根据用户Guid和班级Guid获取测试,status如果不传默认为1
 getTrainingByClass(userID,classID,paperID,status,callback){
     if(typeof status=='undefined') status =1;
     if(!paperID) paperID = '5c21329e-6ff1-4ea5-abbd-532b9c506ded';
     if(!classID) classID = 'null';
        let sql = `SELECT * FROM training WHERE Creater = '${userID}' AND FK_ClassGuid = '${classID}' AND FK_PaperGuid='${paperID}'AND \`Status\`=${status}`;
        query(sql,(err,data)=>{
             if(err){
                 util.writeErr(err+',sql='+sql+'时间='+new Date+','); 
             }else{
                 callback(data);
             }
        })
 }
}